package com.baike.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baike.dao.FileDao;
import com.baike.model.FileObj;


@WebServlet("/DownloadServlet")
public class DownloadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public DownloadServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int file_id =  Integer.parseInt(request.getParameter("file_id"));
		String path=FileDao.getPathById(file_id);
		// 璇诲彇鏂囦欢
		InputStream in = new FileInputStream(path);
		OutputStream out = response.getOutputStream();

		// 鍐欐枃浠�
		int b;
		while ((b = in.read()) != -1) {
			out.write(b);
		}

		FileDao.downloadOnce(file_id);
		in.close();
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}